package com.examples.ry.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TwoTextSizes {
	static final String smallText = "Some small text and ";
	static final FontData smallFD = new FontData("Times", 14, SWT.NORMAL);
	static final String largeText = "Some large text";
	static final FontData largeFD = new FontData("Times", 36, SWT.NORMAL);

	public static void main(String[] args) {
		final Display display = new Display();
		final Font smallFont = new Font(display, smallFD);
		final Font largeFont = new Font(display, largeFD);
		final Shell shell = new Shell(display);
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				Rectangle bounds = shell.getClientArea();
				int top = bounds.y;
				int left = bounds.x;
				System.out.println("Top::"+top+", Left ::"+left);
				GC gc = event.gc;
				
				gc.setFont(largeFont);
				FontMetrics fm = gc.getFontMetrics();
				int largeBaseline = fm.getAscent() + fm.getLeading();
				System.out.println("largeBaseLine::"+largeBaseline);
				
				gc.setFont(smallFont);
				fm = gc.getFontMetrics();
				int smallBaseline = fm.getAscent() + fm.getLeading();
				System.out.println("smallBaseLine::"+smallBaseline);
				
				int yAdvance = largeBaseline - smallBaseline;
				int xAdvance = gc.stringExtent(smallText).x;
				System.out.println("yAdvance, xAdvance::"+yAdvance +","+xAdvance);
				
				gc.drawString(smallText, left,  top+yAdvance, true);
				System.out.println("small text drawn::"+left +",top+yadvance::");
				
				gc.setFont(largeFont);
				gc.drawString(largeText, left + xAdvance,top, true);
				System.out.println("large text drawn, left + xAdvance, top::");
				
				gc.setLineStyle(SWT.LINE_DOT);
				gc.drawLine(left, largeBaseline, left + bounds.width,
						largeBaseline);
			}
		});
		shell.setText("Two Text Sizes");
		shell.setSize(400, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
